<?php
/**
 * Represents a text, without any html entities.
 */
fily_XMLConverter::registerNamespace('text', fily_Text);
class fily_Text implements fily_ChildControl, fily_XMLToControl {
    /**
     * the text
     * @var string
     */
    protected $text;

    /**
     *
     * @param string $text
     */
    public function __construct($text = '') {
        $this->setText($text);
    }

    /**
     * sets the text
     * @param string $text
     */
    public function setText($text) {
        $this->text = htmlspecialchars($text);
    }

    /**
     * returns the html text
     * @return string
     */
    public function toHtml() {
        return $this->getText();
    }

    /**
     * prepares code for html output
     */
    public function prepare() {
        // do nothing
    }

    /**
     * shortcut for toHtml()
     */
    public function __toString() {
        return $this->toHtml();
    }

    /**
     * returns the text
     * @return string
     */
    public function getText() {
        return $this->text;
    }

    /**
     * this control can't have a id
     * it returns a empty string
     * @return string
     */
    public function getId() {
        return '';
    }


      /**
     * system id
     * @var string
     */
    protected $sid;
        /**
     * system id
     * @return string
     */
    public function getSid(){
        return $this->sid;
    }

    /**
     * system id
     * @param string $sid
     */
    public function setSid($sid){
        $this->sid = $sid;
    }

    /**
     * parent from this control
     * @var fily_ControlToHtml
     */
    protected $parent;
    /**
     * returns the parent from this control
     * @return fily_ControlToHtml
     */
    public function getParent() {
        return $this->parent;
    }

    /**
     * set the parent from this control
     * @param fily_ControlToHtml $parent
     */
    public function setParent(fily_ControlToHtml $parent = null) {
        $this->parent = $parent;
    }

    /**
     * finds a control with the specific id
     * @param string $id
     * @return fily_ControlToHtml
     */
    public function getControlById($id) {
        // we don't have sub controls
        return null;
    }
/**
     * finds a control with the specific system id
     * @param string $sid
     * @return fily_ControlToHtml
     */
    public function getControlBySid($sid) {
        return null;
    }

    public function XMLAddControl(fily_ControlToHtml $control) {
    }
    public function XMLAttributes(array $attributes, $value) {
       foreach($attributes as $key => $value) {
            switch ($key) {
               case 'sid':
                    $this->setSid($value);
                    break;
                default:
                    throw new Exception('XML Convert : attribute "'. $key.'" not know for ' .  get_class($this));
                    break;
         }
       }
        $this->setText($value);
    }
    public function XMLCallFunction($name, array $parameters, $innerValue,fily_ControlToHtml $innerControl = null) {
         throw new Exception('XML Convert : function "'. $name.'" not know for ' .  get_class($this));
    }
    public function XMLLateBinding(fily_ControlToHtml $baseControl) {
    }


    public function find($expression) {
        $control = null;
        if(!fily_ExpressionHelper::isExpression($expression)) {
            //expression is an id
            $control = $this->getControlById($expression);
        }
        else {
            $expressionParts = fily_ExpressionHelper::getParts($expression) ;
            // first expression part is always empty
            if(count($expressionParts)>1) {
                switch ($expressionParts[1]) {
                    case 'next':
                        $control =  fily_ExpressionHelper::next($expressionParts, $expression, $this);
                        break;
                    case 'previous':
                        $control = fily_ExpressionHelper::previous($expressionParts, $expression, $this);
                        break;
                    case 'parent':
                        $control = fily_ExpressionHelper::parent($expressionParts, $expression, $this);
                        break;
                }
            }
            // found a control and have another expressions
            if($control!=null && !empty($expression)) {
                $control = $control->find($expression);
            }
        }
        return $control;
    }

}
?>
